package com.fs.web.yz.controller;


import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.fs.common.pojo.Product;
import com.fs.common.pojo.User;
import com.fs.common.yzMapper.ProductMapper;
import com.fs.web.base.enums.CodeEnum;
import com.fs.web.base.utils.ApplicationUtils;
import com.fs.web.base.vo.JsonResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 套餐管理
 */
@Slf4j
@Controller
@RequestMapping("/console/product/rewrite")
public class ProductController {

    @Autowired
    private ProductMapper productMapper;


    @RequestMapping("/loadProduct")
    @ResponseBody
    public JsonResponse loadProduct(){
        try {
            String today = DateUtil.today();
            Map map = new HashMap();
            map.put("date", today);
            User user = ApplicationUtils.getUser().getUser();
            if(StringUtils.isNoneBlank(user.getStartTime()) && StringUtils.isNoneBlank(user.getEndTime())){
                long betweenDay = DateUtil.between(DateUtil.parse(user.getEndTime()), DateUtil.parse(user.getStartTime()), DateUnit.DAY);
                int value = new Long(betweenDay / 365).intValue();
                if(value < 2){
                    map.put("type","two");
                }else{
                    map.put("type","three");
                }
            }else{
                map.put("type","one");
            }
            List<Product> products = productMapper.selectWebProduct(map);
            return JsonResponse.success(products);
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return JsonResponse.error(CodeEnum.EX999);
        }
    }

}
